package com.example.noobpackage.ui.publishpackage

import android.os.Bundle
import android.transition.TransitionInflater
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.AutoCompleteTextView
import android.widget.Toast
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import com.example.noobpackage.R
import com.example.noobpackage.logic.model.ExpressData
import com.example.noobpackage.tool.ExpressIconUtil
import com.example.noobpackage.ui.MainViewModel
import kotlinx.android.synthetic.main.fragment_publish_package.*
import kotlinx.android.synthetic.main.fragment_publish_package.accessNumber
import kotlinx.android.synthetic.main.fragment_publish_package.location
import kotlinx.android.synthetic.main.fragment_publish_package.packageNumber
import kotlinx.android.synthetic.main.fragment_publish_package.typeImage
import kotlinx.android.synthetic.main.package_market_item_card.*

class PublishPackageFragment : Fragment() {
    private val args: PublishPackageFragmentArgs by navArgs()
    private lateinit var express: ExpressData
    private val shareViewModel: MainViewModel by activityViewModels()
    private val viewModel: PublishPackageViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        sharedElementEnterTransition =
            TransitionInflater.from(context).inflateTransition(android.R.transition.move)
                .setDuration(400)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_publish_package, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        // 配置对应的快递信息
        express = shareViewModel.getExpress(args.position)!!

        // 配置不同的transitionName对应列表项
        typeImage.transitionName = "typeImage${args.position}"
        accessNumber.transitionName = "takingCode${args.position}"
        location.transitionName = "takingPlace${args.position}"

        typeImage.setImageResource(ExpressIconUtil.getIconResource(express.expressType))
        accessNumber.text = express.expressTakingNumber
        location.text = express.expressLocation
        packageNumber.text = express.expressNumber

        // 初始化快递类型选择 -- 记得传入Activity的上下文
        val expressTypes = listOf("轻", "中等", "重", "超重")
        val typeAdapter = ArrayAdapter(requireActivity(), R.layout.select_input_item, expressTypes)
        (express_weight_select.editText as AutoCompleteTextView).setText("轻")
        (express_weight_select.editText as AutoCompleteTextView).setAdapter(typeAdapter)

        confirm_publish.setOnClickListener {
            val navController = findNavController()

            // 判断是否需要登录
            if (shareViewModel.userInfo.value == null) navController.navigate(R.id.loginFragment)
            else {
                confirm_publish.isEnabled = false
                viewModel.publishExpress(
                    express,
                    shareViewModel.userInfo.value!!.account,
                    publish_remark.editText!!.text.toString(),
                    (express_weight_select.editText as AutoCompleteTextView).text.toString()
                ).observe(viewLifecycleOwner) {
                    if (it) {
                        // 更新数据并返回
                        shareViewModel.updateOnlineExpress()
                        navController.popBackStack()
                    } else {
                        confirm_publish.isEnabled = true
                        Toast.makeText(context, "发布失败", Toast.LENGTH_SHORT).show()
                    }
                }
            }
        }
    }
}